//
//  VerifyController.m
//  operativeSide-2.0
//
//  Created by 王春程 on 2016/12/15.
//  Copyright © 2016年 王春程. All rights reserved.
//

#import "VerifyController.h"
#import "LogInManager.h"

typedef NS_ENUM(NSInteger, FIELDTAG){
   FIELDTAGNAME = 1050,
    FIELDTAGPHONE = 2050,
    FIELDTAGWORKPLACE = 3050,
    FIELDTAGWORKNUMBER = 4050
};


@interface VerifyController ()<UITextFieldDelegate>
@property (assign, nonatomic)        FIELDTAG fieldTag;

@property (weak, nonatomic) IBOutlet UITextField *userNameField;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *workPlaceField;
@property (weak, nonatomic) IBOutlet UITextField *worknumberField;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;

@end

@implementation VerifyController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self registerForKeyboardNotifications];
    self.confirmBtn.enabled = NO;
    
    self.userNameField.attributedPlaceholder = [[NSMutableAttributedString alloc]initWithString:@"姓名" attributes:@{NSForegroundColorAttributeName:RGB(204, 204, 204)}];
    self.userNameField.delegate = self;
    self.phoneField.attributedPlaceholder = [[NSMutableAttributedString alloc]initWithString:@"电话" attributes:@{NSForegroundColorAttributeName:RGB(204, 204, 204)}];
    self.phoneField.delegate = self;
    self.workPlaceField.attributedPlaceholder = [[NSMutableAttributedString alloc]initWithString:@"工作地" attributes:@{NSForegroundColorAttributeName:RGB(204, 204, 204)}];
    self.workPlaceField.delegate = self;
    self.worknumberField.attributedPlaceholder = [[NSMutableAttributedString alloc]initWithString:@"工号" attributes:@{NSForegroundColorAttributeName:RGB(204, 204, 204)}];
    self.worknumberField.delegate = self;
    
    WS(weakSelf);
   RAC(self.confirmBtn,enabled) =
    [[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        BOOL flag;
        NSString *usernameString = weakSelf.userNameField.text;
        NSString *phoneString = weakSelf.phoneField.text;
        NSString *workplaceString = weakSelf.workPlaceField.text;
        NSString *worknumberString = weakSelf.worknumberField.text;
        flag = (usernameString && usernameString.length != 0) && (phoneString && phoneString.length != 0) && (workplaceString && workplaceString.length != 0) && (worknumberString && worknumberString.length != 0);
        [subscriber sendNext:@(flag)];
        [subscriber sendCompleted];
        return nil;
    }] map:^id _Nullable(NSNumber *value) {
        return @([value boolValue]);
    }];
    
    
    [[self.confirmBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
    
        NSLog(@"confirmClick");
        
    }];
    
    
    // Do any additional setup after loading the view from its nib.
}



#pragma mark -_- TextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    self.fieldTag = textField.tag;
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    [self.userNameField resignFirstResponder];
    [self.phoneField resignFirstResponder];
    [self.worknumberField resignFirstResponder];
    [self.workPlaceField resignFirstResponder];
}


- (void)registerForKeyboardNotifications

{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
     
                                             selector:@selector(keyboardWasShown:)
     
                                                 name:UIKeyboardDidShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
     
                                             selector:@selector(keyboardWillBeHidden:)
     
                                                 name:UIKeyboardWillHideNotification object:nil];
    
}

//实现当键盘出现的时候计算键盘的高度大小。用于输入框显示位置
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    WS(weakSelf);
    CGFloat duration = [aNotification.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
    CGRect frame = [aNotification.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
    
    CGFloat margin ;
    
    switch (self.fieldTag) {
        case FIELDTAGNAME:
        {
            margin = 198.0 + 44.0;
        }
            break;
        case FIELDTAGPHONE:
        {
            margin = 198.0 + 44.0 + 54.0;
            
        }
            break;
        case FIELDTAGWORKPLACE:
        {
            margin = 198.0 + 44.0 + 108.0;
            
        }
            break;
        case FIELDTAGWORKNUMBER:
        {
            margin = 198.0 + 44.0 + 152.0;
            
        }
            break;
        default:
            break;
    }
    
    NSLog(@"%f",margin);
    CGRect rect = self.view.bounds;
    rect.origin.y = margin - frame.origin.y;
    if (rect.origin.y > 0) {
        [UIView animateWithDuration:duration animations:^{
            NSLog(@"%f",rect.origin.y);
            weakSelf.view.bounds = rect;
        }];
    }
}

//当键盘隐藏的时候
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    WS(weakSelf);
    [UIView animateWithDuration:0.3 animations:^{
        CGRect rect = weakSelf.view.bounds;
        rect.origin.y = 0;
        weakSelf.view.bounds = rect;
    }];
}



- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardDidShowNotification
                                                  object:nil];
    
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
